package com.kuang.springboot2board.dao;

import com.kuang.springboot2board.pojo.Department;
import com.kuang.springboot2board.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Gerald
 * @date 2022/6/13-21:01
 */

@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> Employees=null;

    
    @Autowired
    private DepartmentDao departmentDao;

    static {
        Employees=new HashMap<Integer,Employee>();
        Employees.put(1001,new Employee(1001,"AA","A1234344",1,new Department(101,"教学部")));
        Employees.put(1002,new Employee(1002,"BB","B1234344",0,new Department(102,"后勤部")));
        Employees.put(1003,new Employee(1003,"CC","C1234344",1,new Department(103,"学习部")));
        Employees.put(1004,new Employee(1004,"DD","D1234344",0,new Department(104,"运营部")));
        Employees.put(1005,new Employee(1005,"EE","E1234344",1,new Department(105,"销售部")));
    }

    private static Integer initId=1006;

    public void save(Employee employee){
        if(employee.getId()==null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        Employees.put(employee.getId(),employee);
    }

    public Collection<Employee> getAll(){
        return Employees.values();
    }

    public Employee getEmployeeById(Integer id){
        return Employees.get(id);
    }

    public void delete(Integer id){
        Employees.remove(id);
    }

}
